package com.ljc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljc.entity.UserMessage;
import com.ljc.service.UserMessageService;
import com.ljc.service.WsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;

/**
 * @Author: ljc
 * @Date:Created in 16:12 2022/2/17
 */
@Service
public class WsServiceImpl implements WsService {
    @Autowired
    UserMessageService messageService;
    @Autowired
    SimpMessagingTemplate  messagingTemplate;
    //开启异步通知
    @Async

    @Override
    public void     sendMessCountToUser(Long toUserId) {
        int count=messageService.count(new QueryWrapper<UserMessage>()
        .eq("to_user_id",toUserId)
                .eq("status",0));
        System.out.println("count:"+count);
        //websocket通知(/user/20/messCount)
        messagingTemplate.convertAndSendToUser(toUserId.toString(),"/messCount",count);
    }
}
